// 手机端js文件
$(function(){

	const keyCode = {
		"mleftBtn": "ArrowLeft",
		"mupBtn": "ArrowUp",
		"mrightBtn": "ArrowRight",
		"mdownBtn":"ArrowDown",
		"mspaceBtn":" "
	}

	function dispatchKeyEvent(key, type) {
		let evt = new KeyboardEvent(type, {
			bubbles: true,
			cancelable: true,
			key: key,
			code: key,
			// keyCode: message.value, //已弃用
			// shiftKey: true
		});
		// console.log(keyup, message.value, keyCodeMap[message.value])
		document.dispatchEvent(evt);
	}

	$('.nav-list-trigger').click(function(){
		$('body').css('overflow','hidden')
		$('.nav-off-canvas-bg').show()
	})
	$('.close-nav').click(function(){
		$('body').css('overflow','auto')
		$('.nav-off-canvas-bg').hide()
	})

	function onMouseDown(e) {
		let id = e.target.id
		if (keyCode[id]){
			dispatchKeyEvent(keyCode[id], 'keydown')
		}
	}
	function onMouseUp(e) {
		let id = e.target.id
		if (keyCode[id]) {
			dispatchKeyEvent(keyCode[id], 'keyup')
		}
	}

	document.onmousedown = onMouseDown
	document.onmouseup = onMouseUp

	document.ontouchstart = (e) => {
		console.log('touchstart', 'keydown')
		// console.log(e)
		// e.stopPropagation()
		let id = e.target.id
		if (!keyCode[id]) {
			return
		}
		onMouseDown(e)
		try {
			e.preventDefault()
		} catch (error) {
			console.log(error)
		}
	}

	document.ontouchend = (e) => {
		console.log('touchend', 'keyup')
		// console.log(e)
		// e.stopPropagation()
		// onButtonClick(e)
		let id = e.target.id
		if (!keyCode[id]) {
			return
		}
		onMouseUp(e)
		try {
			e.preventDefault()
		} catch (error) {
			console.log(error)
		}
	}
	$(window).load(function(){
		var storage = window.localStorage;
		var c = storage.getItem("c");
		if(c){
			$('.like-this').addClass('active')
		}
	})
	$('.like-this').click(function(){
		if(!window.localStorage){
			alert("对不起，您的浏览器不支持该功能");
		}else{
			var storage = window.localStorage;
			var workid = $('#works_id').val();
			var nowDate = new Date().getTime();
			var c = storage.getItem("c");
			console.log(c)
			if(c && nowDate - c < 86400000){
				alert('您已经支持了哦！')
			}else{
				storage.setItem("c", nowDate);
				$.ajax({
					url:'/h5app/zan',
					type:'POST',
					data:{
						wid:workid
					},
					success:function(){
						$('.like-this').addClass('active');
						let zanNum = parseInt($('#zanNum').text()) + 1;
						$('#zanNum').text(zanNum)
					},
					error:function(){
						alert('操作失败，请刷新重试！')
					}
				})
			}
			
		}
	})
	
	
})

